# -*- encoding: utf-8 -*-
##############################################################################
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as published
#    by the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see http://www.gnu.org/licenses/.
#
##############################################################################

from datetime import datetime
from odoo.exceptions import ValidationError
from odoo.tools import float_compare
from odoo.exceptions import except_orm, Warning, RedirectWarning
import odoo.addons.decimal_precision as dp
from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT
from odoo import api, fields, models, _


class stock_move(models.Model):
    _inherit = ['stock.move']

    replace_products = fields.Many2many('mrp.product.replace', string=u'替代料', compute="_get_replace_product")

    @api.multi
    @api.depends('product_id')
    def _get_replace_product(self):
        for move in self:
            move.replace_products = self.env['mrp.product.replace'].search(
                [('name', '=', move.product_id.id), ('mrp_bom_line_id', '=', move.bom_line_id.id)])

    @api.multi
    def action_choose_replace_products(self):
        view = self.env['ir.model.data'].xmlid_to_res_id('mrp_product_replace.view_choose_product_form')
        choose_obj = self.env['choose.product']
        wiz_id = self.env['choose.product'].create({'stock_move_id': self.ids[0]})
        return {
            'name': _('choose.product'),
            'type': 'ir.actions.act_window',
            'view_type': 'form',
            'view_mode': 'form',
            'res_model': 'choose.product',
            'views': [(view, 'form')],
            'view_id': view,
            'target': 'new',
            'res_id': wiz_id.id,
        }
        return {'type': 'ir.actions.act_window_close'}
